﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace NDBGen
{
    internal class ScriptedTableDataDefinition
    {
        public ScriptedTableDataDefinition(String sourceFilePath)
        {
            this.SourceFilePath = sourceFilePath;
        }

        public String SourceFilePath { get; private set; }
        public String TableDefLine { get; set; }
        public String ColumnDefLine { get; set; }
        public List<String> DataLines { get; set; }
    }

    public class ScriptedTableData
    {
        public ScriptedDataColumn[] Columns { get; set; }
        public ScriptedDataEntry[] Entries { get; set; }

        public String TableSchemaName { get; set; }
        public String TableName { get; set; }

        public TableKey TableKey { get { return new TableKey(this.TableSchemaName, this.TableName); } }

        internal static ScriptedTableData Parse(ScriptedTableDataDefinition tableDef)
        {
            var scriptedData = new ScriptedTableData();

            var tableLineParts = tableDef.TableDefLine.Split(new String[] { "#", "." }, StringSplitOptions.RemoveEmptyEntries);
            scriptedData.TableSchemaName = tableLineParts[0];
            scriptedData.TableName = tableLineParts[1];

            var columnDefs = (
                from columnPart in tableDef.ColumnDefLine.Split(new String[] { "|" }, StringSplitOptions.RemoveEmptyEntries)
                select columnPart.Trim()).ToArray();

            scriptedData.Columns = ScriptedDataColumn.Parse(columnDefs);
            scriptedData.Entries = tableDef.DataLines.Select(l => ScriptedDataEntry.Parse(tableDef.SourceFilePath, scriptedData.Columns, l)).ToArray();

            return scriptedData;
        }
    }
}